<?php

class Admin_Form_Property_Edit extends My_FormAdmin {

    protected $property;

    public function __construct($property, $options = null) {
        $this->property = $property;
        parent::__construct($options);
    }

    public function init() {
        $this->setMethod('post');
        $this->setAction($this->getView()->url(array('controller' => 'property', 'action' => 'save', 'id' => $this->property['property_id']), 'admin', true));
        $this->setTitle($this->translate('XXX_ADMIN_PROPERTY_EDIT_FORM_TITLE'));

        $caption = new My_Form_Element_Content('tmp');
        $caption->setValue($this->property['display_name'] . ' <small>(' . $this->property['name'] . ')</small>');
        $this->addElement($caption);

        $caption = new My_Form_Element_Content('internal');
        $caption->setLabel(_('XXX_ADMIN_PROPERTY_FORM_INTERNAL_NAME_LABEL'));
        $caption->setValue($this->property['name']);
        $this->addElement($caption);

        $defaultValue = $this->property['default_value'];
        $type = $typeStr = $this->property['type'];
        
        switch ($type) {
            case 'text':
                $value = new Zend_Form_Element_Text('value');
                $typeStr = 'Wartość tekstowa';
                break;
            case 'boolean':
                $value = new Zend_Form_Element_Select('value');
                $value->setMultiOptions(array('0' => 'Nie', '1' => 'Tak'));
                $typeStr = 'Wartość logiczna';
                break;
            case 'integer':
                $value = new Zend_Form_Element_Text('value');
                $value->addValidator(new Zend_Validate_Int());
                $typeStr = 'Liczba całkowita';
                break;
            case 'float':
                $value = new Zend_Form_Element_Text('value');
                $filter = new Zend_Filter_PregReplace(array(
                    'match' => '/\./',
                    'replace' => ',',
                ));
                $value->addFilter($filter);
                $value->addValidator(new Zend_Validate_Float());
                $typeStr = 'Liczba całkowita';
                break;

            default:
                $value = new Zend_Form_Element_Text('value');
                break;
        }


        $value->setLabel(_('XXX_ADMIN_PROPERTY_FORM_VALUE_LABEL'));
        $this->addElement($value);


        $caption = new My_Form_Element_Content('type');
        $caption->setLabel(_('XXX_ADMIN_PROPERTY_FORM_DESCRIPTION_LABEL'));
        $caption->setValue($typeStr);
        $this->addElement($caption);

        $caption = new My_Form_Element_Content('def_val');
        $caption->setLabel(_('XXX_ADMIN_PROPERTY_FORM_DEFAULT_VALUE_LABEL'));
        $caption->setValue($defaultValue);
        $this->addElement($caption);

        $caption = new My_Form_Element_Content('desc');
        $caption->setLabel(_('XXX_ADMIN_PROPERTY_FORM_DESCRIPTION_LABEL'));
        $caption->setValue($this->property['description']);
        $this->addElement($caption);

        $id = new Zend_Form_Element_Hidden('id');
        $id->setValue($this->property['property_id']);
        $this->addElement($id);

        $this->addSubmitElement();
    }

    public function getValues($suppressArrayNotation = false) {
        $data = parent::getValues($suppressArrayNotation);
        if ($this->property['type'] == 'float') {
            $data['value'] = str_replace(',', '.', $data['value']);
        }
        return $data;
    }

}

